package com.lovo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.lovo.bean.RoleThrone;
import com.lovo.bean.Throne;
import com.lovo.bean.ThroneDistribution;
import com.lovo.bean.User;
import com.lovo.service.RoleThroneService;
import com.lovo.service.ThroneService;
import com.lovo.service.serviceImpl.RoleThroneServiceImpl;
import com.lovo.service.serviceImpl.ThroneServiceImpl;
import com.lovo.util.CodeMsg;
import com.lovo.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ThroneServlet",urlPatterns = "/throne.do")
public class ThroneServlet extends HttpServlet {
    private ThroneService ts = new ThroneServiceImpl();
    private RoleThroneService rts = new RoleThroneServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if (method == null || "".equalsIgnoreCase(method)) {
            String page = request.getParameter("page");
            int pageNo = 0;
            if (page == null || "".equalsIgnoreCase(page)) {
                pageNo = 1;
            } else {
                pageNo = Integer.parseInt(page);
            }
            String limit = request.getParameter("limit");
            PageInfo<Throne> pageInfo = ts.getThrones(pageNo, Integer.parseInt(limit));
            Result<PageInfo<Throne>> result = null;
            if (pageInfo == null) {
                result = Result.error(CodeMsg.NOTFOUND_ERROR);
            } else {
                result = Result.success(pageInfo);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("query")){
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String content = request.getParameter("content");
            PageInfo<Throne> pageInfo = ts.getThronesByQuery(Integer.parseInt(page), Integer.parseInt(limit),content);
            Result<PageInfo<Throne>> result = null;
            if (pageInfo == null) {
                result = Result.error(CodeMsg.NOTFOUND_ERROR);
            } else {
                result = Result.success(pageInfo);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("addThrone")){
            String throneName = request.getParameter("throneName");
            String thronePath = request.getParameter("thronePath");
            String throneDesc = request.getParameter("throneDesc");

            Throne throne = new Throne();
            throne.setThroneDesc(throneDesc);
            throne.setThroneName(throneName);
            throne.setThronePath(thronePath);

            boolean flag = ts.addThrone(throne);
            Result<String> result = Result.success(String.valueOf(flag));
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("deleteThrone")){
            String throneId = request.getParameter("throneId");
            System.out.println("throneId = " + throneId);
            boolean flag = ts.deleteThrone(Integer.parseInt(throneId));
            Result<String> result = Result.success(String.valueOf(flag));
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("updateThrone")){
            String throneId = request.getParameter("throneId");
            String throneDesc = request.getParameter("throneDesc");
            String throneName = request.getParameter("throneName");
            String thronePath = request.getParameter("thronePath");
            Throne t = new Throne();
            t.setThronePath(thronePath);
            t.setThroneName(throneName);
            t.setThroneDesc(throneDesc);
            t.setThroneId(Integer.parseInt(throneId));
            boolean flag = ts.updateThrone(t);
            Result<String> result = Result.success(String.valueOf(flag));
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("throneDistribution")){
             PageInfo<ThroneDistribution> pageInfo =  ts.getAllThrones();
            Result<PageInfo<ThroneDistribution>> result = null;
            if (pageInfo == null){
                result = Result.error(CodeMsg.NOTFOUND_ERROR);
            } else {
                result = Result.success(pageInfo);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("getRoleThrone")){
            String roleId = request.getParameter("roleId");
            PageInfo<Integer> pageInfo =  ts.getRoleThrone(Integer.parseInt(roleId));
            Result<PageInfo<Integer>> result = null;
            if (pageInfo == null){
                result = Result.error(CodeMsg.NOTFOUND_ERROR);
            } else {
                result = Result.success(pageInfo);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("changeThrone")){
            String value = request.getParameter("value");
            String roleId = request.getParameter("roleId");
            boolean flag = ts.deleteRoleThrone(roleId);
            boolean flag1 = rts.addRoleThrone(value,roleId);

            if(flag && flag1){
                Result<String> result = Result.success(String.valueOf(flag));
                ObjectMapper objectMapper = new ObjectMapper();
                String json = objectMapper.writeValueAsString(result);
                response.getWriter().write(json);
            }else{
                Result<String> result = Result.error(CodeMsg.SERVER_ERROR);
                ObjectMapper objectMapper = new ObjectMapper();
                String json = objectMapper.writeValueAsString(result);
                response.getWriter().write(json);
            }

        }else if(method.equals("getThroneById")){
            getThroneById(request,response);
        }else if(method.equals("getThroneListByIdStr")){
            getThroneListByIdStr(request,response);
        }
    }

    private void getThroneListByIdStr(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String throneIdStr = request.getParameter("throneIdStr");
        throneIdStr="("+throneIdStr.substring(0,throneIdStr.length()-1)+")";
        System.out.println("throneIdStr = " + throneIdStr);
        List<Throne> thrones = ts.selectByIdStr(throneIdStr);
        String json = new Gson().toJson(thrones);
        response.getWriter().write(json);

    }

    private void getThroneById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("throneId");
        int throneId =0;
        if(id!=null){
            throneId =Integer.parseInt(id);
        }
        Throne throne = ts.getThroneById(throneId);
        String json = new Gson().toJson(throne);
        response.getWriter().write(json);

    }
}
